define(["jquery", "easy-admin"], function ($, ea) {

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'system.admin/index',
        add_url: 'system.admin/add',
        edit_url: 'system.admin/edit',
        delete_url: 'system.admin/delete',
        modify_url: 'system.admin/modify',
        export_url: 'system.admin/export',
        password_url: 'system.admin/password',
        selectuser_url: 'system.admin/selectuser',
    };

    var Controller = {

        index: function () {

            ea.table.render({
                init: init,
                cols: [[
                    {type: "checkbox"},
                    {field: 'id', width: 80, title: 'ID',search: false},
                    {field: 'sort', width: 80, title: '排序', edit: 'text',search: false},
                    {field: 'username', minWidth: 80, title: '登录账户'},
                    {field: 'head_img', minWidth: 80, title: '头像', search: false, templet: ea.table.image},
                    {field: 'phone', minWidth: 80, title: '手机'},
                    {field: 'login_num', minWidth: 80, title: '登录次数',search: false},
                    {field: 'auth_name', minWidth: 80, title: '角色权限',search: false},
                    // {field: 'remark', minWidth: 80, title: '备注信息'},
                    {field: 'status', title: '状态', width: 85, search: 'select', selectList: {0: '禁用', 1: '启用'}, templet: ea.table.switch},
                    {field: 'create_time', minWidth: 80, title: '创建时间', search: 'range'},
                    {
                        width: 250,
                        title: '操作',
                        templet: ea.table.tool,
                        operat: [
                            'edit',
                            [{
                                text: '设置密码',
                                url: init.password_url,
                                method: 'open',
                                auth: 'password',
                                class: 'layui-btn layui-btn-normal layui-btn-xs',
                            }],
                            'delete'
                        ]
                    }
                ]],
            });

            ea.listen();
        },
        add: function () {
            ea.listen();
        },
        edit: function () {
            ea.listen();
        },
        password: function () {
            ea.listen();
        },
        selectuser: function () {
            ea.table.render({
                init: init,
                toolbar: [[{
                    text: '确认选择',
                    method: 'none',
                    //   auth: 'add',
                    class: 'layui-btn layui-btn-normal layui-btn-sm selectuserbtn',
                    //   icon: 'fa fa-plus ',
                    //   extend: 'data-full="true"',
                }],'refresh'],
                cols: [[

                    {type: 'checkbox'},

                    {field: 'id', title: 'id', search: false },

                    {field: 'username', title: '登录账号'},

                    {field: 'phone', title: '手机号'},
                    // {field: 'auth_ids', title: '权限',search: 'select', selectList: {8: '禁用'}},
                    {field: 'head_img', title: '头像', templet: ea.table.image, search: false,},

                ]],
            });

            $('body').on('click',".selectuserbtn",function(){

                //  $("input[name='layTableRadio_1']").each(function(){

                //      $(this.)
                //         console.log($(this));
                //  });
                var checkStatus = layui.table.checkStatus("currentTableRenderId"),
                    data = checkStatus.data;
                if (data.length <= 0) {
                    layer.msg('未选择任何数据');
                    return false;

                }
                // alert(4343);return false;
                $user_ids = [];
                for (let index=0; index < data.length; index++) {
                    console.log(data[index].id);
                    $user_ids.push(data[index].id)

                }

                parent.$(".selectuser").val( $user_ids.toString());

                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引

                // if(parent.$(".selectuser").val()){
                //
                //     $.getJSON("/admin/user.member/userinfo?id="+parent.$(".selectuser").val(), function (data){
                //
                //         var showuser = parent.$(".selectuser").parent('div').find(".showuser");
                //
                //         if(showuser.length && showuser.length>0){
                //
                //             showuser.find('img').attr('src',data.headpath);
                //
                //             showuser.find('b').html(data.nickname);
                //
                //         }else{
                //
                //             var html = "<div class='showuser'><span><image src='"+data.headpath+"' style='width:50px;'></span><b>"+data.nickname+"</b></div>"
                //
                //             parent.$(".selectuser").parent('div').append(html);
                //
                //         }
                //
                //     })
                //
                // }


                parent.layer.close(index); //再执行关闭

            })


            ea.listen();
        },
    };
    return Controller;
});